<!DOCTYPE html>
<html>
<head>
<title>Module Develop</title>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<style type="text/css">
	html,body{
		height: 100%
	}
	body{
		background-color: #fff;
	    margin: 0;
	}
	#wrap{
	    height: 100%;
	    position: relative;
	}
	#header{
		background-color: #5082c2;
		height: 44px;
	    position: relative;
	}
	#header h1{
	    font-size: 20px; 
	    height: 44px; 
	    line-height: 44px; 
	    margin: 0em; 
	    color: #fff;
		margin-left: 100px;
		margin-right: 100px;
		text-align: center;
	}
  #main{
      display: block;
  }
  a.button{
      display: -webkit-box;
      -webkit-box-pack: center;
      -webkit-box-align: center;
      height: 36px;
      margin: 8px;
      background-color: rgba(240,240,240,1.0);
      border-color: rgba(220,220,220,1.0);
      border-width: 2px;
      border-style: solid;
	}
  a.active{
      background-color: rgba(240,240,240,0.6);
	}
</style>
</head>
<body>
	<div id="wrap">
	    <header id="header">
	    	<h1 id="title">企业微信SDK模块</h1>
	    </header>
	    <div id="main">
            <a class="button" tapmode="active" onclick="isInstalledWorkWX()">是否安装了企业微信</a>
			<a class="button" tapmode="active" onclick="openWorkWX()">打开企业微信</a>
			<a class="button" tapmode="active" onclick="registerWorkWXSDK()">注册企业微信SDK</a>
			<a class="button" tapmode="active" onclick="shareText()">分享文本到企业微信</a>
			<a class="button" tapmode="active" onclick="shareImage()">分享图片到企业微信</a>
			<a class="button" tapmode="active" onclick="shareFile()">分享文件到企业微信</a>
			<a class="button" tapmode="active" onclick="shareVideo()">分享视频到企业微信</a>
			<a class="button" tapmode="active" onclick="shareLink()">分享链接到企业微信</a>
        </div>
    </div>
</body>
<script>
	function isInstalledWorkWX() {
		var workWXModule = api.require('workWX')
		workWXModule.isInstalled(function(ret, err) {
			// 10000 	- 未安装企业微信
			// 0 		- 已安装企业微信
			console.log(JSON.stringify(ret))
			api.toast({
				msg: ret.msg,
				location: 'middle'
			})
		})
	}
	
	function openWorkWX() {
		var workWXModule = api.require('workWX')
		workWXModule.openApp(function(ret, err) {
			// 10000 	- 未安装企业微信
			// 0		- 打开企业微信成功
			// -1		- 打开企业微信失败
			console.log(JSON.stringify(ret))
			api.toast({
				msg: ret.msg,
				location: 'middle'
			})
		})
	}
	
	// actually has registed when app onlaunch, if not set in config, it'll use default one
	// if not pass them as param, it will fetch them in config.xml
	function registerWorkWXSDK() {
		var workWXModule = api.require('workWX')
		workWXModule.registerApp(function(ret, err) {
			// -1 - 缺少必须的ID
			// 0  - 注册成功
			// -1 - 注册失败
			console.log(JSON.stringify(ret))
			api.toast({
				msg: ret.msg,
				location: 'middle'
			})
		})
	}
	
	function shareText() {
		var workWXModule = api.require('workWX')
		workWXModule.shareText({
			text: 'share text here ...'
		}, function(ret, err) {
			// 10000 	- 未安装企业微信
			// 400000	- 分享文本为空
			// 0 		- 分享成功
			// 20000	- 分享失败
			console.log(JSON.stringify(ret))
			api.toast({
				msg: ret.msg,
				location: 'middle'
			})
		})
	}
	
	function shareImage() {
		api.getPicture({
			sourceType: 'album',
			encodingType: 'jpg',
			mediaValue: 'pic',
			destinationType: 'url',
			allowEdit: true,
			quality: 50
		}, function(ret, err) {
			if (ret) {
				console.log(JSON.stringify(ret))
				// ret.data - url
				var workWXModule = api.require('workWX')
				workWXModule.shareImage({
					imgName: 'demo.png',
					imgPath: ret.data
				}, function(ret, err) {
					// 10000 	- 未安装企业微信
					// 400001	- 分享图片为空
					// 20001	- 分享失败
					console.log(JSON.stringify(ret))
					api.toast({
						msg: ret.msg,
						location: 'middle'
					})
				})
			} else {
				console.log(JSON.stringify(err))
			}
		})
	}
	
	function shareFile() {
		const filePathTxt = 'fs://demo.txt'
		api.writeFile({
			path: filePathTxt,
			data: 'share to wxwork'
		}, function(ret, err) {
			if (ret.status) {
				var workWXModule = api.require('workWX')
				workWXModule.shareFile({
					fileName: 'demo.txt',
					filePath: filePathTxt
				}, function(ret, err) {
					// 10000 				- 未安装企业微信
					// 400003				- 分享文件为空
					// 20002(wrong here)	- 分享失败
					console.log(JSON.stringify(ret))
					api.toast({
						msg: ret.msg,
						location: 'middle'
					})
				})
			}
		})
	}
	
	function shareVideo() {
		api.getPicture({
			sourceType: 'album',
			mediaValue: 'video',
			destinationType: 'url',
			allowEdit: true,
			quality: 50
		}, function(ret, err) {
			if (ret) {
				console.log(JSON.stringify(ret))
				// ret.data - url
				var workWXModule = api.require('workWX')
				workWXModule.shareVideo({
					videoName: 'demo.mp4',
					videoPath: ret.data
				}, function(ret, err) {
					// 10000 	- 未安装企业微信
					// 400002	- 分享音视频为空
					// 20002	- 分享失败
					console.log(JSON.stringify(ret))
					api.toast({
						msg: ret.msg,
						location: 'middle'
					})
				})
			} else {
				console.log(JSON.stringify(err))
			}
		})
	}

	function shareLink() {
		var workWXModule = api.require('workWX')
		workWXModule.shareLink({
			title: 'share title',
			summary: 'share summary',
			url: 'https://mp.weixin.qq.com/s/8kYy0vx45f1GN971B0obKw',
			iconUrl: 'https://mmbiz.qpic.cn/mmbiz_png/z2Au4ia6XoicbcQVRh5kcqPUdSF7Dq2Lpo5u5UwYxCEpGeeMdyjicmpC7ldI1TwjtmibnX3GHZhibqXMeLE24BX1szQ/640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1'
		}, function(ret, err) {
			// 10000 	- 未安装企业微信
			// 400004	- 分享链接为空
			// 20004	- 分享失败
			console.log(JSON.stringify(ret))
			api.toast({
				msg: ret.msg,
				location: 'middle'
			})
		})
	}
	
	function apiready() {
		var header = document.getElementById('header');
		header.style.paddingTop = api.safeArea.top + 'px';

		api.removeLaunchView();
	}
</script>
</html>
